#ifndef SOUNDENGINE_H
#define SOUNDENGINE_H

#define POP_SOUND					"Music/random/pop.ogg"

#define AMBIENT_SOUND_1				"Music/ambient/music/calm1.ogg"
#define AMBIENT_SOUND_2				"Music/ambient/music/calm2.ogg"

#define BLOCK_BREAK_SOUND_GRASS		"Music/dig/grass1.ogg"
#define BLOCK_BREAK_SOUND_DIRT		"Music/dig/gravel1.ogg"
#define BLOCK_BREAK_SOUND_SAND		"Music/dig/sand1.ogg"
#define BLOCK_BREAK_SOUND_STONE		"Music/dig/stone1.ogg"
#define BLOCK_BREAK_SOUND_WOOD		"Music/dig/wood4.ogg"

/////////////
// LINKING //
/////////////
#pragma comment(lib, "dsound.lib")

//////////////
// INCLUDES //
//////////////
#include "DXUT.h"
#include <dsound.h>
#include <map>

#include    <vorbis/codec.h>            // from the vorbis sdk
#include    <vorbis/vorbisfile.h>       // also :)

#define     BUFSIZE    65536			// buffer length

class SoundEngine
{
protected:
	int			id;
	struct Sound {
		LPDIRECTSOUNDBUFFER		pDSB;                   // the buffer
		OggVorbis_File			vf;                     // for the vorbisfile interface
		int						nLastSection;           // which half of the buffer are/were
		int						nCurSection;            // we playing?
		bool					bAlmostDone;            // only one half of the buffer to play
		bool					bDone;                  // done playing
		bool					bLoop;                  // loop?

								Sound();
		void					Open( SoundEngine * engine, char *filename );
		void                    Close();
		void                    Play(bool loop = false);
		void                    Stop();
		bool                    Update();
		bool					IsPlaying() {return !bDone;}
	};

    bool        bInitialized;           // initialized?
    bool        bFileOpened;            // have we opened an ogg yet?

    bool        bReleaseDS;             // release ds by ourselves?

    LPDIRECTSOUND8
                pDS;                    // the directsound 8 object

    LPDIRECTSOUNDBUFFER
                pDSB;                   // the buffer

    OggVorbis_File 
                vf;                     // for the vorbisfile interface

    int         nLastSection,           // which half of the buffer are/were
                nCurSection;            // we playing?

    bool        bAlmostDone;            // only one half of the buffer to play
    bool        bDone;                  // done playing
    bool        bLoop;                  // loop?

	std::map<int, Sound>	m_Sounds;
	float		fUpdateTime;
public:

    SoundEngine();
    ~SoundEngine();

    bool                                // initialize dsound ..
        InitDirectSound( HWND hWnd );
 
    inline void                         // .. or use already initialized
        UseDirectSound( LPDIRECTSOUND8 _pDS )
    {
        pDS = _pDS;
    }

    bool                                // this opens an oggvorbis for playing
        OpenOgg( char *filename );

    void                                // and this one closes it :)
        Close();

    void                                // play it again sam
        Play(bool loop = false);

    void                                // stop it
        Stop();

    void                                // be sure to call this from time to time
        Update();                       

    inline bool IsPlaying()
    {
        return !bDone;
    }

	void OnFrameMove( double fTime, float fElapsedTime);
	void Quickplay( char *filename );
};

#endif